<?php
namespace application\datatypes;
use application\datatypes\DataType;

class LongText extends DataType {
	public static $name = 'Long Text';
	public static $fields = array(array('columnType'=>'TEXT'));
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && !$data[$field->cf_column]) {
			$results['errors'][] = $field->cf_display_name.' is required.';
		}
		elseif($value = $data[$field->cf_column]) {
			$results['db_fields'][$field->cf_column] = $value;	
		}
		
		return $results;
	}
	
	public static function displayJS($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(CN_URL.'modules/tish/tish.jquery.js');
		?><link rel="stylesheet" type="text/css" media="all" href="<?= CN_ROOT_URL?>modules/tish/default.css" />
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?> .tish').tish();
			});
		</script><?php
	}
	
	public static function formJS($field) {
		switch($field->cf_aux) {
			case 'wysiwyg':
				Modulator()->includeScript('jquery');
				Modulator()->includeScript('jquery ui');
				Modulator()->includeScript(getCommonFile('third-party/plupload/js/plupload.full.js'));
				Modulator()->includeScript(getCommonFile('third-party/plupload/js/jquery.plupload.queue/jquery.plupload.queue.js'));
				?>
				
				<script type="text/javascript" src="<?=getCommonFile('third-party/wymeditor/jquery.wymeditor.js')?>"></script>
				<script type="text/javascript" src="<?=getCommonFile('concerto/wymeditor/concerto-buttons.js')?>"></script>
				<script type="text/javascript">
					$(document).ready(function() {
						$('#<?=$field->cf_column?>').wymeditor({
							stylesheet: '<?=getCommonFile('concerto/css/wymeditor.css')?>',
							postInit: function(wym) {
								wym.concertoButtons('<?=CN_INCLUDES_URL?>', '<?=CN_URL?>');
		 
					            
							}
						});
					});
				</script>
				<?php
				break;
			case 'html':
			default:
				break;
				
		}
			
		
	}
	
	public static function display($formElements) {
		?>
		<fieldset class="single-field">
			<legend><?=$formElements['display']?></legend>
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = $data[$field->cf_column];
		/**switch($field->cf_aux) {
			case 'html':
				$class = 'html-editor';
				break;
			case 'wysiwyg':
				$class = 'wysiwyg-editor';
				break;
			default:
				$class = 'long-text';
				break;
		}*/
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
				'element'=>
					sprintf('<textarea class="%1$s" name="%2$s" id="%2$s">%3$s</textarea>', 'editor', $field->cf_column, $value)
				
		));
		return $formElements;
	
	}
	
	public static function add() {
	
	}
	
	/**public static function save() {
		$editorType = $_REQUEST['editor_type'];
		$field = $_REQUEST['field'];
		DB()->query("UPDATE `cn_content_fields` SET `cf_aux`='$editorType' WHERE `cf_id`='$field'");
	}*/
	
	public static function delete() {
	
	}
	
	public static function prepareData($field, $data, $params) {
		require CN_DOCUMENT_ROOT.'modules/tish/TishHighlighter.php';
		$tish = new \TishHighlighter;
		$content = $data[$field->cf_column];
		// Get all content within code tags
		
		
		
		$codeBlocks = \TishHighlighter::getBlocksBetween(':\<pre class="code"\>:', ':\<\/pre\>:', $content, TRUE);
		
		foreach($codeBlocks as $block) {
			$clean = str_replace('<pre class="code">', '', $block);
			$clean = str_replace('</pre>', '', $clean);
			ob_start();
			$tish->parse(html_entity_decode($clean));
			$html = ob_get_clean();
			$html = '<pre class="tish"><div class="tish-inner">'.$html.'</div></pre>'.'<div style="clear:both;"></div>'; 
			$content = str_replace($block, $html, $content);
			
		}
		return '<div id="'.$field->cf_column.'">'.$content.'</div>';
	}
	
	public static function editForm($field) {
		?>
		<div class="box-content">
			<form method="post">
			<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
			<p>
			<label>Field Display Name</label>
			<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</p>
			<p>
			<label>Field Description</label>
			<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</p>
			<h4>Editor Type</h4>
				<label><input type="radio" name="field_aux" checked="checked" value="wysiwyg"/> WYSIWYG (What you see is what you get)</label>
				<label><input type="radio" <?php if($field->getAux() == 'html') echo 'checked="checked"';?> name="field_aux" value="html"/> HTML</label>
				<label><input type="radio" <?php if($field->getAux() == 'none' || !$field->getAux()) echo 'checked="checked"';?> name="field_aux" value="none"/> None</label>
			
			<p><input type="submit" name="update_field" class="small-button" value="Submit Changes"/></p>
			</form>
		</div>
			<?php
	}	
	
}
?>